var REGEX = {
	checkHideExMobileNo:/^1\d{10}$/	  ///^(13|15|17|18|14)[0-9][*]{4}[0-9]{4}$/
}

$(function(){
    $.ajaxSetup({cache:false});
    $("#exValidDate").blur(function(){
        checkExValidDate();
    });

    $("#exCvn2").blur(function(){
        checkExCvn2();
    });

    $("#exHolder").blur(function(){
        checkExHolder();
    });

    $("#exIdCardNo").blur(function(){
        checkExIdCardNo();
    });

    $("#exMobileNo").blur(function(){
        checkExMobileNo();
    });
    $("#hideMobileNo").blur(function(){
    	checkHideExMobileNo();
    	var before=$("#hideMobileNo").attr("initData");
    	var after=$("#hideMobileNo").val();
    	if(before!=after){
    		$("#exMobileNo").attr("value",after);
    	}
    });
    
//    $("#hideMobileNo").change(function(){
//    	REGEX.checkHideExMobileNo = /^(13|15|17|18|14)[0-9]{9}$/;
//    });
    
    $("#exSmsCode").blur(function(){
        checkExSmsCode();
    });

    $("#agreeOpen").click(function(){
        checkAgreeOpen();
    });
    // 快捷支付改造修改  update by dhj at 20161201 start
    /*$("#exIdCardNo6").blur(function(){
        checkExIdCardNo6();
    });*/
    // 快捷支付改造修改  update by dhj at 20161201 end
});


function checkExValidDate(){
    $("#exValidDateTip").hide();
    var vd = $("#exValidDate").val();
    if($.trim(vd)==""){
        $("#exValidDateTip").html("请输入有效期").show();
        return false;
    }
    var month = vd.substr(0,2);
    var year = vd.substr(2,2);
    var curYear = new Date().getFullYear() - 2000;
    var curMonth = new Date().getMonth()+1;
    if((Number(month) > 12 || Number(month) < 1)|| (Number(month) < curMonth && Number(year) <=curYear) ||(!(Number(year) >= curYear && Number(year) <= curYear+20))){
        $("#exValidDateTip").html("有效期格式有误").show();
        return false;
    }
    return true;
}

function checkExCvn2(){
	var isOpendCVN2=$("#isOpendCVN2").val();
	if(isOpendCVN2=="yes"){
	    $("#exCvn2Tip").hide();
	    if($.trim($("#exCvn2").val()) == ''){
	        $("#exCvn2Tip").html("请输入CVN2").show();
	        return false;
	    }
	    if((!/^[0-9]{3}$/.test($("#exCvn2").val()))){
	        $("#exCvn2Tip").html("CVN2格式有误").show();
	        return false;
	    }
	    return true;
	}else{
		return true;
	}
}

function checkExHolder(){
    $("#exHolderTip").hide();
    if($.trim($("#exHolder").val()) == ''){
        $("#exHolderTip").html("请输入持卡人姓名").show();
        return false;
    }
    if(/^\s*$/.test($("#exHolder").val())){
        $("#exHolderTip").html("持卡人姓名格式有误").show();
        return false;
    }
    return true;
}

function checkExIdCardNo(){
	var ageFlag=false;
	$("#sendSmsBtn").attr("disabled",false);
    $("#exIdCardNoTip").hide();
    if($.trim($("#exIdCardNo").val()) == ''){
        $("#exIdCardNoTip").html("请输入身份证号").show();
        return false;
    }
    
    var rd = $("#exIdCardNo").attr('initData');
    if (rd && $.trim(rd) != '') {
    	return true;
    }
    
    if(!checkCard($("#exIdCardNo").val())){
        $("#exIdCardNoTip").html("身份证号格式有误").show();
        return false;
    }

    var exIdCardNo = $("#exIdCardNo").val();
    if(typeof(setMaxDigits) === 'function'){
        bodyRSA();
        exIdCardNo = encryptedString(key, encodeURIComponent(exIdCardNo));
    }

    jQuery.ajax({
    	type:"post",
    	url:ctx+"/00100/express/checkAdult.shtml",
    	async:false,
    	dataType:"json",
    	data:{exIdCardNo:$.trim(exIdCardNo)},
    	success:function(data){
    		if(data.result){
    			ageFlag=true;
    			return false;
    		}
    	}
    });
    if(ageFlag){
    	$("#exIdCardNoTip").html("您还未满18周岁，无法继续支付").show();
    	ageFlag=false;
    	$("#sendSmsBtn").attr("disabled",true);
    	return false;
    }
    return true;
}

function checkExMobileNo(){
    $("#exMobileNoTip").hide();
    if($.trim($("#exMobileNo").val()) == ''){
        $("#exMobileNoTip").html("请输入银行预留手机号").show();
        return false;
    }
    if(!(/^1\d{10}$/.test($("#exMobileNo").val()))){
        $("#exMobileNoTip").html("银行预留手机号格式有误").show();
        return false;
    }
    return true;
}
function checkHideExMobileNo(){
    $("#exMobileNoTip").hide();
    if($.trim($("#hideMobileNo").val()) == ''){
        $("#exMobileNoTip").html("请输入银行预留手机号").show();
        return false;
    }
    if($("#hideMobileNo").attr("initData") == $("#hideMobileNo").val()){
    	return true;
    }
    if(!(REGEX.checkHideExMobileNo.test($("#hideMobileNo").val()))){
        $("#exMobileNoTip").html("银行预留手机号格式有误").show();
        return false;
    }
    return true;
}

function checkExSmsCode(){
    $("#exSmsCodeTip").hide();
    if($.trim($("#exSmsCode").val()) == ''){
        $("#exSmsCodeTip").html("请输入短信验证码").show();
        return false;
    }
    if($.trim($("#exSmsCode").val()).length !=6){
        $("#exSmsCodeTip").html("短信验证码有误").show();
        return false;
    }
    return true;
}

function checkAgreeOpen(){
    $("#agreeOpenTip").hide();
    if(!($('input:checkbox[id="agreeOpen"]').is(":checked"))){
        $("#agreeOpenTip").show();
        return false;
    }
    return true;
}
//快捷支付改造修改  update by dhj at 20161201 start
/*function checkExIdCardNo6(){
    $("#exIdCardNo6Tip").hide();
    if(!(/^[A-Za-z0-9]{6}$/.test($("#exIdCardNo6").val()))){
        $("#exIdCardNo6Tip").html("身份证号后六位有误。").show();
        return false;
    }
    return true;
}*/
//快捷支付改造修改  update by dhj at 20161201 end
function commonPayCheck(){
    var exPayChannel = $("#exPayChannel").val();
    var  checkRes = true;
    if(exPayChannel == "06"){
        checkRes = checkExValidDate() && checkExCvn2();
    }
    checkRes = checkRes && checkExHolder()&&checkExIdCardNo()&&checkExMobileNo()&&checkExSmsCode();
    return checkRes;
}

function commonPayCheckNoSms(){
    var exPayChannel = $("#exPayChannel").val();
    var  checkRes = true;
    //快捷支付改造修改  update by dhj at 20161201 start
    //首次绑卡时才校验绑卡要素
    if($("#loginExFirstPay").val()=="01"){
    	if(exPayChannel == "06"){
    		checkRes = checkExValidDate() && checkExCvn2();
    	}
    	checkRes = checkRes && checkExHolder()&&checkExIdCardNo()&&checkExMobileNo();
    }else{
    	if(exPayChannel == "06"){
    		checkRes = checkExCvn2();
    	}
    }
    //快捷支付改造修改  update by dhj at 20161201 end
    return checkRes;
}